﻿using System.Linq;

namespace Wicresoft.Core.Mvc.Pager
{
    /// <summary>
    ///     mvc分页 Linq 扩展
    ///     <para>2010/8/13</para>
    ///     <para>TANGJINGBO</para>
    ///     <para>tangjingbo</para>
    /// </summary>
    public static class PageLinqExtensions
    {
        /// <summary>
        ///     Toes the paged list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="allItems">All items.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public static PagedList<T> ToPagedList<T>
            (
            this IQueryable<T> allItems,
            int pageIndex,
            int pageSize
            )
        {
            if (pageIndex < 1)
                pageIndex = 1;
            int itemIndex = (pageIndex - 1)*pageSize;
            IQueryable<T> pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
            int totalItemCount = allItems.Count();
            return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount);
        }
    }
}